package com.naiterui.ehp.bs.patient.dao.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.naiterui.ehp.bp.domain.Hospital;
import com.naiterui.ehp.bp.utils.lang.StringUtil;
import com.naiterui.ehp.bs.patient.dao.IHospitalDao;
import com.naiterui.ehp.bs.patient.repository.IApHospitalRepository;

/**
 * 医院持久化实现
 * <p>
 * File name : HospitalDao.java
 * </P>
 * <p>
 * Author : NTR.Amber
 * </P>
 * <p>
 * Date : 2015年9月11日
 * </P>
 */
@Component
public class ApHospitalDao implements IHospitalDao {

    @Autowired
    private IApHospitalRepository hospitalRepository;

    @Override
    public List<Hospital> getHospitalList(List<Integer> cityIds, String name) {
        if (!StringUtils.isEmpty(name)) {
            return hospitalRepository.findByNameLike(name);
        } else {
            StringBuilder ids = new StringBuilder();
            for (Iterator<Integer> iter = cityIds.iterator(); iter.hasNext(); ) {
                Integer id = iter.next();
                if (iter.hasNext()) {
                    ids.append(id.toString()).append(",");
                } else {
                    ids.append(id.toString());
                }
            }
            return hospitalRepository.findByHideAndCityIdInOrderByCreatedAtDesc(0, cityIds);
        }
    }

    /**
     * @param hospitalIds
     *
     * @return
     *
     * @Author Gaoll
     * @Date 2016年8月12日
     * @since HZD-1.0.0
     */
    @Override
    public Map<Integer, Hospital> getHospitalMap(Collection<? extends Number> hospitalIds) {

        Map<Integer, Hospital> resultMap = new HashMap<>();

        String queryStr = StringUtil.convertCollection(hospitalIds);
        if (StringUtils.isNotBlank(queryStr)) {
            List<Hospital> hospitalList = hospitalRepository.findByIdIn((Collection<Integer>) hospitalIds);
            if (null != hospitalList && !hospitalList.isEmpty()) {
                for (Hospital tempHospital : hospitalList) {
                    resultMap.put(tempHospital.getId(), tempHospital);
                }
            }
        }
        return resultMap;
    }

    @Override
    public Hospital get(Integer id) {
        return hospitalRepository.get(id);
    }
}
